<template>
  <div>
    <el-dialog
      :title="isUpdate ? '编辑' : '新建'"
      v-model="props.visible"
      width="50%"
      @close="closeDialog()"
    >
      <el-form
        :model="form"
        label-position="right"
        label-width="100px"
        ref="formRef"
        @submit.prevent
        :rules="rules"
      >
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="归属系统" prop="repBody">
              <el-radio-group v-model="form.modelName" @change="radioHandler()">
                <el-radio :value="'hx'">红线</el-radio>
                <el-radio :value="'wg'">网关</el-radio>
                <el-radio :value="'md'">美盾</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20" v-if="form.modelName != 'wg'">
          <el-col :span="17" :offset="1">
            <el-form-item label="进件标识：" prop="appNo">
              <el-input
                placeholder="请输入进件号|身份证"
                v-model="form.appNo"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="系统标识：" prop="appCode">
              <el-input
                placeholder="请输入call_name|serviceCode"
                v-model="form.appCode"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="响应报文：" prop="repBody">
              <el-input
                placeholder="请输入响应报文"
                type="textarea"
                resize="none"
                :rows="15"
                v-model="form.repBody"
              ></el-input>
              
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="响应状态" prop="repFlag">
              <el-radio-group
                v-model="form.repFlag"
                v-if="form.modelName == 'wg'"
              >
                <el-radio :value="3">
                  <el-tag size="large" type="success"> 查得成功 </el-tag>
                </el-radio>
                <el-radio :value="4">
                  <el-tag size="large" type="warning"> 未查得 </el-tag>
                </el-radio>
                <el-radio :value="5">
                  <el-tag size="large" type="danger"> 失败 </el-tag>
                </el-radio>
              </el-radio-group>

              <el-radio-group v-model="form.repFlag" v-else>
                <el-radio :value="1">
                  <el-tag size="large" type="success"> SUCCESS </el-tag>
                </el-radio>
                <el-radio :value="2">
                  <el-tag size="large" type="danger"> FAIL </el-tag>
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="18" :offset="1">
            <el-form-item label="是否启用" prop="isOpen">
              <el-switch
                v-model="form.isOpen"
                inline-prompt
                active-text="启用"
                inactive-text="关闭"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-row :gutter="20"
          ><el-col :span="17" :offset="1">
            <div
              class="dialog-footer"
              style="display: flex; justify-content: left; margin-bottom: 30px"
            >
              <el-button type="primary" @click="save()">确 定</el-button>
              <el-button @click="closeDialog()">取 消</el-button>
            </div>
          </el-col>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { dsAgreementApi } from "@/api/kernel/ds-agreement.js";

const { proxy } = getCurrentInstance();

const formRef = ref(null);
const props = defineProps({
  visible: Boolean,
  data: Object,
});
const emits = defineEmits(["update:visible", "done"]);

const radioHandler = () => {
  if (form.value.modelName == "wg") {
    form.value.repFlag = 3;
  } else {
    form.value.repFlag = 1;
  }
};

const form = ref({
  isOpen: true,
  repFlag: 1,
  modelName: "hx",
});
const rules = ref({
  appNo: [{ required: true, message: "进件标识不能为空", trigger: "blur" }],
  appCode: [{ required: true, message: "系统标识为空", trigger: "blur" }],
  repBody: [{ required: true, message: "响应报文不能为空", trigger: "blur" }],
  repFlag: [{ required: true, message: "请选择响应状态", trigger: "blur" }],
});
onMounted(async () => {
  if (props.data) {
    getInfoById(props.data);
    isUpdate.value = true;
  } else {
    isUpdate.value = false;
  }
});
const getInfoById = (data) => {
  let { trId } = data;
  dsAgreementApi.getInfoById(trId).then((res) => {
    let { repFlag } = res.data;
    let repFlagInt = parseInt(repFlag);
    form.value = Object.assign({}, res.data);
    form.value.repFlag = repFlagInt;
  });
};

// 是否是编辑状态
const isUpdate = ref(false);

const closeDialog = () => {
  updateVisible(false);
  emits("done");
};
// 更改弹框状态
const updateVisible = (value) => {
  emits("update:visible", value);
};

//#- 保存
const loading = ref(false);

const save = () => {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      loading.value = true;
      if (isUpdate.value) {
        dsAgreementApi.edit(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
        });
      } else {
        dsAgreementApi.add(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          console.log("add:response", response);
        });
      }
      loading.value = false;
      closeDialog();
    }
  });
};
</script>
